package com.cf.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.pojo.Team;
import com.cf.domain.pojo.TeamMember;
import com.cf.domain.pojo.User;
import com.cf.domain.vo.TeamVO;
import com.cf.domain.vo.UserVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.awt.geom.QuadCurve2D;
import java.util.List;

public interface UserMapper extends BaseMapper<User> {

    @Update("update t_user set email = #{email}, name = #{name}, area = #{area} where id = #{id}")
    int updateUser(@Param("id") Long id, @Param("name") String name, @Param("email") String email, @Param("area") Integer area);

    @Update("update t_user set password = #{password} where id = #{id}")
    int updatePwd(@Param("id") Long id, @Param("password") String password);

    @Select("SELECT id, name, phone, email, area,type FROM t_user WHERE is_lock = 0")
    Page<User> selectList(Page<User> page, QueryWrapper<User> queryWrapper);

    @Select("SELECT id, name, phone, email, area, type, source FROM t_user ${ew.customSqlSegment}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "team", column = "id", one = @One(select = "com.cf.mapper.TeamMapper.getTeamByUserId", fetchType= FetchType.DEFAULT))
    })
    Page<TeamVO> selectTeamList(Page<TeamVO> page, @Param(Constants.WRAPPER) Wrapper<TeamVO> queryWrapper);

    @Select("SELECT id, name, phone, email, area, type, source FROM t_user ${ew.customSqlSegment}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "team", column = "id", one = @One(select = "com.cf.mapper.TeamMapper.getTeamByUserId", fetchType= FetchType.DEFAULT)),
            @Result(property = "members", javaType = List.class, column = "id", many = @Many(select = "com.cf.mapper.TeamMemberMapper.getMembersByUserId"))
    })
    List<TeamVO> selectMembers(@Param(Constants.WRAPPER) Wrapper<TeamVO> queryWrapper);

    @Select("SELECT id, name, phone, email, area, type, source FROM t_user ${ew.customSqlSegment}")
    @Results({
            @Result(property = "team", column = "id", one = @One(select = "com.cf.mapper.TeamMapper.getTeamByUserId", fetchType= FetchType.DEFAULT)),
            @Result(property = "members", javaType = List.class, column = "id", many = @Many(select = "com.cf.mapper.TeamMemberMapper.getMembersByUserId"))
    })
    TeamVO selectMember(@Param(Constants.WRAPPER) Wrapper<TeamVO> queryWrapper);


    @Select("SELECT id, name, phone, email, area, type FROM t_user WHERE id = #{userId}")
    UserVO getUserById(Long userId);

}
